package com.tubang.logistics.controller;


import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tubang.common.util.BeanMapper;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.ResultData;
import com.tubang.component.util.RequestUtil;
import com.tubang.logistics.entity.PictureEntity;
import com.tubang.logistics.service.IPictureService;
import com.tubang.logistics.vo.PictureVo;

/**
 * <p>
 * 图片表 前端控制器
 * </p>
 * @author wsl
 * @since 2017-12-28
 */
@RestController
@RequestMapping("/picture")
public class PictureController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private IPictureService pictureService;
	
	/**
	 * @Title insertOrUpdate 
	 * @Description 图片新增或者修改
	 * @param  request
	 * @param  response
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@PostMapping(value = "insertOrUpdate")
	public ResultData insertOrUpdate(HttpServletRequest request, HttpServletResponse response) {
		//获取数据
		String receivePostJson = RequestUtil.receivePostJson(request);
		PictureVo form = GsonQuick.toObject(receivePostJson, PictureVo.class);
		
		try {
			PictureEntity pictureEntity = BeanMapper.map(form, PictureEntity.class);
			pictureEntity.setUpdateTime(new Date());
			pictureService.insertOrUpdate(pictureEntity);
			logger.info("[picture]<== method[insertOrUpdate],新增或者修改的数据为 ：[{}] ", GsonQuick.getGson().toJson(pictureEntity));
			return ResultData.result(true);
		} catch (Exception e) {
			logger.error("[picture]<== method[insertOrUpdate], 新增或者修改失败 :[{}]", e.getMessage());
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}
	
	/**
	 * @Title delete 
	 * @Description 根据主键删除图片
	 * @param @param pictureId
	 * @param @return 设定文件 
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@GetMapping(value = "delete")
	public ResultData delete(@RequestParam(required = true) String pictureId) {
		try {
			PictureEntity pictureEntity = new PictureEntity();
			pictureEntity.setId(pictureId);
			pictureEntity.setUpdateTime(new Date());
			pictureEntity.setEnable(false);
			pictureService.updateById(pictureEntity);
			logger.info("[picture]<== method[delete],删除成功，图片主键为 ：[{}] ", pictureId);
			return ResultData.result(true);
		} catch (Exception e) {
			logger.error("[picture]<== method[delete], 删除异常 :[{}]", e.getMessage());
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}
	
	/**
	 * @Title selectByPictureId 
	 * @Description 根据主键查询图片信息
	 * @param @param pictureId
	 * @param @return 设定文件 
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@GetMapping(value = "selectByPictureId")
	public ResultData selectByPictureId(@RequestParam(required = true) String pictureId) {
		
		try {
			PictureEntity pictureEntity = pictureService.selectById(pictureId);
			logger.info("[picture]<== method[selectByPictureId],根据主键查询 ：[{}] ", GsonQuick.getGson().toJson(pictureEntity));
			return ResultData.result(true).setData(pictureEntity);
		} catch (Exception e) {
			logger.error("[picture]<== method[selectByPictureId], 根据主键查询失败 :[{}]", e.getMessage());
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}
	
	/**
	 * @Title selectByPictureId 
	 * @Description 展示图片列表
	 * @param  pictureId
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@GetMapping(value = "selectPictureList")
	public ResultData selectPictureList(PictureVo pictureVo) {
		try {
			logger.info("[picture]==> method[selectMessageList],请求数据为：[{}]", GsonQuick.getGson().toJson(pictureVo));
			PictureEntity pictureEntity = BeanMapper.map(pictureVo, PictureEntity.class);
			EntityWrapper<PictureEntity> ew = new EntityWrapper<>(pictureEntity);
			Page<PictureEntity> page = new Page<>(pictureVo.getPage(),pictureVo.getRows());
			
			Page<PictureEntity> selectPage = pictureService.selectPage(page,ew);
			int totalCount = pictureService.selectCount(ew);
			logger.info("[picture]==> method[selectPictureList],应答：[{}]", GsonQuick.getGson().toJson(selectPage));
			return ResultData.result(true).setTotalCount(totalCount).setData(selectPage.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[picture]==> method[selectPictureList],应答：[{}]", e.getMessage());
			return ResultData.result(false).setData(e);
		}
	}
	
}
